# 多阶段构建 - 后端
FROM maven:3.8.6-openjdk-11 AS backend-build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline -B
COPY src ./src
RUN mvn clean package -DskipTests

# 多阶段构建 - 前端
FROM node:18-alpine AS frontend-build
WORKDIR /app
COPY frontend/package*.json ./
RUN npm ci --only=production
COPY frontend/ ./
RUN npm run build

# 生产镜像
FROM openjdk:11-jre-slim
WORKDIR /app

# 复制后端jar包
COPY --from=backend-build /app/target/ai-chat-system-1.0.0.jar app.jar

# 复制前端构建文件
COPY --from=frontend-build /app/dist ./static

# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser
RUN chown -R appuser:appuser /app
USER appuser

# 暴露端口
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:8080/api/health || exit 1

# 启动应用
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "app.jar"]